//
//  SEImageCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/7.
//

import UIKit

class SEImageCell: UICollectionViewCell {
    
    private var viewModel: SEInquiryInfoViewModel?
    private var deleteImageClosure: ((SEInquiryInfoViewModel) -> Void)?
    private var currentIndexPath: IndexPath?
    
    func update(with viewModel: SEInquiryInfoViewModel, indexPath: IndexPath, isCanEdit: Bool, deleteImage: ((SEInquiryInfoViewModel) -> Void)?) {
        self.viewModel = viewModel
        self.deleteImageClosure = deleteImage
        self.currentIndexPath = indexPath
        deleteBtn.isHidden = viewModel.deleteButtonHidden(isCanEdit: isCanEdit, at: indexPath)
        if viewModel.isAddImageCell(isCanEdit: isCanEdit, at: indexPath) {
            imageView.image = Asset.iconAddImage.image
        } else {
            if isCanEdit {
                if let dataImg = viewModel.dataToImage(at: indexPath) {
                    imageView.image = dataImg
                } else {
                    imageView.kf.setImage(urlString: viewModel.info.picList?[indexPath.item].shareUrl)
                }
            } else {
                imageView.kf.setImage(urlString: viewModel.info.picList?[indexPath.item].shareUrl)
            }
        }
    }
    
    @IBAction private func deleteImage() {
        guard let viewModel = viewModel, let indexPath = currentIndexPath else { return }
        if indexPath.item <= viewModel.imagesCount {
            viewModel.info.picList?.remove(at: indexPath.item)
        }
        deleteImageClosure?(viewModel)
    }
    
    @IBOutlet private weak var imageView: UIImageView! {
        didSet {
            imageView.setLayerCorners(rect: imageView.bounds, radius: 4)
        }
    }
    
    @IBOutlet private weak var deleteBtn: UIButton!

}
